﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Expo.Training.Entity;
using Expo.Training.BRule;
using System.IO;


public partial class Special_AddSpecial : PageBase
{
    public string strFile
    {
        get
        {
            if (ViewState["FileName"] != null)
            {
                return ViewState["FileName"].ToString();  
            }
            else
            {
                return "";
            }
        }
        set
        {
            ViewState["FileName"] = value;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }



    public void DoAdd(object sender, System.EventArgs e)
    {
        Special_Info objsi = new Special_Info();
        string Title = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string Content = txt_Content.Text.Trim();
        DateTime topExpiredDate = DateTime.Parse("1900-1-1");

        #region Check
        if (Title.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_BulletinT_error\").innerText = \"标题不能为空！\";");
            return;
        }
        if (Content.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_new_error\").innerText = \"内容不能为空！\";");
            return;
        }
        if (Content.Length > 150)
        {
            AddEndScript("input error", "$(\"sp_new_error\").innerText = \"专项推送内容不能超过150字！\";");
            return;
        }
        #endregion

        objsi.SpecialTitle = Title;
        objsi.SpecialContent = Content;
        objsi.CustomerID = this.Customer.CustomerID;
        objsi.Creator = this.Member.MemberID;

        #region 保存图片及图片路径
        string folderName = ResolveClientUrl("~/Data/Special/");
        if (!Directory.Exists(Server.MapPath(folderName)))
        {
            Directory.CreateDirectory(Server.MapPath(folderName));
        }
        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
        string filePath = Server.MapPath(folderName + fileName + fileExtension);
        if (fileExtension != "" || fileExtension == null)
        {
            if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
            {
                this.AddEndScript("error", "alert('不支持的图片格式！');");
                return;
            }
            this.File1.PostedFile.SaveAs(filePath);
            CutImage image = new CutImage();
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n" + fileExtension), 40, 25);
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_p" + fileExtension), 210, 132);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            objsi.ImageUrl = fileName + "_p" + fileExtension;
        }
        else
        {

            objsi.ImageUrl = "Default.gif";

        }

        #endregion

        #region 首页置顶

        #region 首页缩略图文件参数赋值
        folderName = ResolveClientUrl("~/Data/Special/HomePage/");
        if (!Directory.Exists(Server.MapPath(folderName)))
        {
            Directory.CreateDirectory(Server.MapPath(folderName));
        }
        fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        fileExtension = Path.GetExtension(this.File2.PostedFile.FileName).ToUpper();
        filePath = Server.MapPath(folderName + fileName + fileExtension);
        #endregion

        if (CK_Top.Checked)
        {
            objsi.IsTop = 1;
            objsi.Number = Convert.ToInt32(ddlNumber.SelectedValue);

            #region 保存首页缩略图

            if (fileExtension != "" || fileExtension == null)
            {
                if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
                {
                    this.AddEndScript("error", "alert('不支持的图片格式！');");
                    return;
                }
                this.File2.PostedFile.SaveAs(filePath);
                CutImage image = new CutImage();
                image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n" + fileExtension), 114, 17);
                image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_p" + fileExtension), 228, 34);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                objsi.HomePageImageUrl = fileName + "_p" + fileExtension;
            }
            else
            {

                objsi.HomePageImageUrl = "Default.gif";

            }

            #endregion
        }
        else
        {
            objsi.IsTop = 0;
            objsi.Number = 999;
            //设置首页缩略图默认名称
            objsi.HomePageImageUrl = "Default.gif";
        }

        #endregion

        objsi.Add();
        Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "专项推送管理 - 添加 - ID:" + objsi.SpecialID);
        AddEndScript("add succes", "alert('添加成功！');location.href='Default.aspx'");


        



    }

    protected void txt_Content_TextChanged(object sender, EventArgs e)
    {

    }
    protected void CK_Top_CheckedChanged(object sender, EventArgs e)
    {
        //首页置顶未选中,序号和首页默认图片输入框设为不可用
        //if (!CK_Top.Checked)
        //{
        //    ddlNumber.Enabled = false;
        //    File2.Disabled = true;
        //}
        //else
        //{
        //    ddlNumber.Enabled = true;
        //    File2.Disabled = false;
        //}
    }
}